﻿// Silverlight Vista Sidebar Gadget Template created by Ioan Lazarciuc
// http://www.lazarciuc.ro/ioan
// Contact form present on website
// Based on the Vista Sidebar Gadget Template created by Tim Heuer 
// http://timheuer.com/blog/

using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Browser;
using SilverlightGadgetUtilities;

namespace WordPressStatsSettings
{
    public partial class Page : UserControl
    {
        SilverlightGadgetEvents evHelper;
        public Page()
        {
            InitializeComponent();
            evHelper = new SilverlightGadgetEvents("SilverlightGadgetEvents");
            evHelper.SettingsClosing += new EventHandler<SettingsClosingEventArgs>(evHelper_SettingsClosing);
            evHelper.SettingsClosed += new EventHandler<SettingsClosingEventArgs>(evHelper_SettingsClosed);

            // Set settings page size according to the one specified in the Silverlight control.
            SilverlightGadget.SetPageSize(Width, Height);

            // Load the settings
            this.txtBlogUri.Text = SilverlightGadget.Settings[SilverlightGadget.SettingsKeys.BlogUri];
            this.txtAPIKey.Text = SilverlightGadget.Settings[SilverlightGadget.SettingsKeys.BlogAPIKey];
            this.numUpDownUpdateFrequency.Value = int.Parse(SilverlightGadget.Settings[SilverlightGadget.SettingsKeys.UpdateFrequency]);
            this.chkBxIncludePublishedPosts.IsChecked = bool.Parse(SilverlightGadget.Settings[SilverlightGadget.SettingsKeys.IncludePublishedPosts]);

            switch (SilverlightGadget.Settings[SilverlightGadget.SettingsKeys.Period])
            {
                case "Month":
                    this.radBtnMonth.IsChecked = true;
                    break;
                case "Week":
                    this.radBtnWeek.IsChecked = true;
                    break;
                default:
                    this.radBtnDay.IsChecked = true;
                    break;
            }            

            this.numUpDownDays.Value = int.Parse(SilverlightGadget.Settings[SilverlightGadget.SettingsKeys.Days]);

            switch (SilverlightGadget.Settings[SilverlightGadget.SettingsKeys.ChartType])
            {
                case "Column":
                    this.radBtnColumn.IsChecked = true;
                    break;
                case "Line":
                    this.radBtnLine.IsChecked = true;
                    break;
            }
        }

        void evHelper_SettingsClosed(object sender, SettingsClosingEventArgs e)
        {
           
        }

        private void UserControl_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            // If through code the size of the Silverlight control is changed, change the size in the javascript gadget also.
            SilverlightGadget.SetPageSize(e.NewSize.Width, e.NewSize.Height);
        }

        void evHelper_SettingsClosing(object sender, SettingsClosingEventArgs e)
        {
            if (e.Action == CloseAction.Commit)
            {
                // Save the settings
                SilverlightGadget.Settings[SilverlightGadget.SettingsKeys.BlogUri] = this.txtBlogUri.Text;
                SilverlightGadget.Settings[SilverlightGadget.SettingsKeys.BlogAPIKey] = this.txtAPIKey.Text;
                SilverlightGadget.Settings[SilverlightGadget.SettingsKeys.UpdateFrequency] = ((int)this.numUpDownUpdateFrequency.Value).ToString();
                SilverlightGadget.Settings[SilverlightGadget.SettingsKeys.Days] = ((int)this.numUpDownDays.Value).ToString();
                SilverlightGadget.Settings[SilverlightGadget.SettingsKeys.IncludePublishedPosts] = this.chkBxIncludePublishedPosts.IsChecked.Value.ToString();

                string period = "Day";
                if (radBtnWeek.IsChecked.Value)
                    period = "Week";
                else if (radBtnMonth.IsChecked.Value)
                    period = "Month";

                SilverlightGadget.Settings[SilverlightGadget.SettingsKeys.Period] = period;

                string chartType = "Line";
                if (radBtnColumn.IsChecked.Value)
                    chartType = "Column";

                SilverlightGadget.Settings[SilverlightGadget.SettingsKeys.ChartType] = chartType;
            }
        }

        private void radBtnDay_Checked(object sender, RoutedEventArgs e)
        {
            if (this.lblDays != null)
                this.lblDays.Content = "day(s)";            
        }

        private void radBtnWeek_Checked(object sender, RoutedEventArgs e)
        {
            if (this.lblDays != null)
                this.lblDays.Content = "week(s)";            
        }

        private void radBtnMonth_Checked(object sender, RoutedEventArgs e)
        {
            if (this.lblDays != null)
                this.lblDays.Content = "month(s)";
        }

        private void linkBtnAPIKey_Click(object sender, RoutedEventArgs e)
        {
            if (this.txtBlogUri.Text.Equals(string.Empty))
            {
                MessageBox.Show("Please enter your WordPress blog uri.", "WordPress Stats", MessageBoxButton.OK);
            }
            else
            {
                this.linkBtnAPIKey.NavigateUri = new Uri(this.txtBlogUri.Text + "/wp-admin/users.php?page=grofiles-user-settings");
                //MessageBox.Show("You will now be taken to a page that will display your WordPress blog API key.  Copy and paste it here in the settings.", "WordPress Stats", MessageBoxButton.OK);                
            }
        }
    }
}
